package com.sgb.member.weight;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.R;
import com.sgb.member.databinding.LayoutUploadFileBinding;
import com.sgb.member.util.ChooseFileUtil;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

public class UploadFileLayout extends LinearLayout {

    private LayoutUploadFileBinding mFileBinding;
    private UploadFileAdapter adapter;

    private SimpleResult<UploadFileLayout> simpleResult;

    public UploadFileLayout(Context context) {
        this(context, null);
    }

    public UploadFileLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UploadFileLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mFileBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.layout_upload_file, null, false);
        if (attrs != null) {
            @SuppressLint("Recycle") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UploadFileLayout);
            boolean required = typedArray.getBoolean(R.styleable.UploadFileLayout_uploadShowDTitle, true);
            mFileBinding.tvTitle.setVisibility(required ? VISIBLE : GONE);
            String title = typedArray.getString(R.styleable.UploadFileLayout_uploadTitle);
            if (!TextUtils.isEmpty(title)) {
                mFileBinding.tvTitle.setText(title);
            }
            int backgroundColor = typedArray.getColor(R.styleable.UploadFileLayout_uploadBackground, 0XFFFFFFFF);
            mFileBinding.fileRootView.setBackgroundColor(backgroundColor);
            typedArray.recycle();
        }
        mFileBinding.fileRecycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new UploadFileAdapter(getContext(), new ArrayList<>(), null);
        mFileBinding.fileRecycler.setAdapter(adapter);
        mFileBinding.fileRecycler.setNestedScrollingEnabled(false);
        mFileBinding.fileRecycler.setFocusable(false);
        mFileBinding.fileRecycler.setFocusableInTouchMode(false);
        mFileBinding.tvUpload.setOnClickListener(v -> {
            if (adapter == null || adapter.getDatas().size() < 5) {
                if (simpleResult != null) {
                    simpleResult.onResult(this);
                }
                ChooseFileUtil.startFileIntent((Activity) v.getContext());
            } else {
                MToast.showToast(getContext(), "最多新增5个附件");
            }
        });
        addView(mFileBinding.getRoot(), new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public void setSimpleResult(SimpleResult<UploadFileLayout> simpleResult) {
        this.simpleResult = simpleResult;
    }

    public List<UploadFileBean> getData() {
        return adapter.getDatas();
    }

    public void addData(UploadFileBean fileBean) {
        adapter.addData(fileBean);
    }

    /**
     * @param beans 数据源
     * @param delete delete != null ? 自定义删除 : 自动删除
     */
    public void setData(List<UploadFileBean> beans, SimpleResult<Integer> delete) {
        adapter = new UploadFileAdapter(getContext(), beans, delete);
        mFileBinding.fileRecycler.setAdapter(adapter);
    }
}
